package com.ac.input.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.ac.input.web.form.DataForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class EdtScriptTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private String formName;

	public EdtScriptTag() {
		super();
	}

	public void release() {
		super.release();
		formName = null;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getFormName() {
		return formName;
	}

	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		StringBuffer results = new StringBuffer("");
		HttpSession session = pageContext.getSession();
		DataForm myForm = (DataForm) session.getAttribute(formName);

		results.append(getStartElement());

		results.append("function validateDataForm(form) {\n return true");
		results.append(" && validateRequired(form)");
		results.append(" && validateMaxLength(form)");
		results.append(" && validateDate(form)");
		results.append(" && validateInteger(form)");
		results.append(" && validateFloat(form)");
		results.append(" && validateOther(form)");
		results.append(";\n}\n");

		List dmList = myForm.getRhid().getCols();
		results.append(ValidateFactory.getNeedResults(dmList));
		results.append(ValidateFactory.getMaxResults(dmList));
		results.append(ValidateFactory.getDateResults(dmList));
		results.append(ValidateFactory.getIntResults(dmList));
		results.append(ValidateFactory.getFloatResults(dmList));
		results.append(ValidateFactory.getOtherResults(dmList));

		results.append(getEndElement());
		try {
			pageContext.getOut().print(results);
		} catch (IOException ex) {
			ex.printStackTrace();
			throw new JspException("Error - tag.getProperty : IOException ");
		}
		return EVAL_PAGE;
	}

	protected String getStartElement() {
		return "<script type=\"text/javascript\">\n<!-- Begin\n";
	}

	protected String getEndElement() {
		return "\n//End -->\n</script>\n";
	}
}
